package org.snail.department.service.listener;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.snail.constant.GroupConstant;
import org.snail.department.service.impl.DeptInfoOptionService;
import org.snail.dept.bean.DepartDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * 钉钉部门API调用对列
 */
@Component("deptInfoListener")
public class DeptInfoListener implements MessageListener {

    private static final Logger LOGGER = Logger.getLogger(DeptInfoListener.class);

    @Autowired
    private DeptInfoOptionService deptInfoOption;

    @Override
    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            LOGGER.info("=======================进行activemq  dept存储操作,消息内容：" + textMessage);
            String text = textMessage.getText();
            DepartDO departDO = JSON.parseObject(text, DepartDO.class);
            LOGGER.info("`````````````ghb:departDO:"+departDO);
            /**
             *  1、没有传入部门信息
             *  2、同步部门信息
             *  3、同步部门下人员信息
             *  4、其他异常错误。
             */
            if (null == departDO) {
                LOGGER.error("activemq中，json转换异常！");
            } else if(departDO.getDeptStatus() == GroupConstant.GROUP_TYPE) {
                deptInfoOption.synchronizeDept(departDO);
            } else if(departDO.getDeptStatus() == GroupConstant.CHILD_GROUP_LIST_TYPE) {
                deptInfoOption.synchronizeDepatEmp(departDO);
            } else if(departDO.getDeptStatus() == GroupConstant.DEPT_MANAGER_TYPE) {
                deptInfoOption.synchronizeDepartManager(departDO);
            } else {
                LOGGER.error("DepartDO对象有错" + departDO);
            }
        } catch (JMSException e) {
            LOGGER.error("钉钉接口保存至部门信息  activemq错误!", e);
        } catch (Exception e) {
            LOGGER.error(e);
        }
    }

}
